package com.wangjiacai.hqlQuery.repository.impl;


import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;

public class HibernateBaseRepositoryImpl extends AbstracterBaseRepositoryImpl {

    @Autowired
    private SessionFactory sessionFactory;

    public HibernateBaseRepositoryImpl(){

    }

    public HibernateBaseRepositoryImpl(SessionFactory sessionFactory){
        this.sessionFactory=sessionFactory;
    }
    @Override
    Query createQuery(String hql) {
        return this.sessionFactory.getCurrentSession().createQuery(hql);
    }

    @Override
    @Transactional
    public <T> T save(T t) {
        this.sessionFactory.getCurrentSession().saveOrUpdate(t);
        return t;
    }

    @Override
    Query createNativeQuery(String sql) {
        return  this.sessionFactory.getCurrentSession().createNativeQuery(sql);
    }
}
